package com.gzyj.jtt808.v2011.protocol;

import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.Package;
import com.gzyj.jtt808.v2011.enums.BusinessDataType;
import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.io.Serializable;
import java.nio.charset.Charset;

/**
 * 驾驶员身份信息采集上报
 *
 * @author lhj
 */
@Data
public class JTT808_0x0702 extends Package implements Serializable {
    private String DriverUserName;//驾驶员姓名
    private String DriverIdentityCard;//驾驶员身份证号 长度20 不足补 '\0'
    private String QualificationCode;//从业资格证编码 2011 长度40 位 ，不足补 '\0'；
    private String LicenseIssuing;//发证机构名称

    public JTT808_0x0702() {
        super(BusinessDataType.驾驶员身份信息采集上报.getCode());
    }

    @Override
    public void encodeImpl(ByteBuf buf) {
        buf.writeByte(this.DriverUserName.getBytes(Charset.forName("GBK")).length);
        buf.writeCharSequence(this.DriverUserName, Charset.forName("GBK"));
        buf.writeBytes(HexBytesUtil.WriteStringPadRight(this.DriverIdentityCard,20));
        buf.writeBytes(HexBytesUtil.WriteStringPadRight(this.QualificationCode, 40));
        buf.writeByte(this.LicenseIssuing.getBytes(Charset.forName("GBK")).length);
        buf.writeCharSequence(this.LicenseIssuing, Charset.forName("GBK"));
    }

    @Override
    public JTT808_0x0702 decodeImpl(ByteBuf buf) {
        int len = Integer.parseInt(HexBytesUtil.bytesToHex(HexBytesUtil.readByOffset(buf, 1)), 16);
        this.DriverUserName = HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf, len));
        this.DriverIdentityCard = HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf, 20));
        this.QualificationCode = HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf, 40));
        len = Integer.parseInt(HexBytesUtil.bytesToHex(HexBytesUtil.readByOffset(buf, 1)), 16);
        this.LicenseIssuing = HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf, len));


        return this;
    }


}
